/**
 * Created by Ysssssss on 2021/5/24.
 */

import { put, takeEvery } from 'redux-saga/effects'
import apiType, {
    BULK_ADD_PRODUCT_TO_SHELF_GROUP,
    BULK_ADD_SHELF_PRODUCT,
    BULK_DELETE_PRODUCT_FROM_SHELF_GROUP,
    BULK_DELETE_SHELF_PRODUCT,
    BULK_EDIT_SHELF_PRODUCT_PRICE,
    BULK_EDIT_SHELF_PRODUCT_STATUS, CHECK_MENU_INFO,
    CREATE_SHELF_GROUP,
    DELETE_SHELF_GROUP,
    EDIT_SHELF_GROUP,
    EDIT_SHELF_PRODUCT_INFO,
    GET_CAN_IMPORT_TO_SHELF_PRODUCT_SELECTION,
    GET_SHELF_GROUP_LIST,
    GET_SHELF_GROUP_SELECTION,
    GET_SHELF_PRODUCT_INFO,
    GET_SHELF_PRODUCT_LIST,
    GET_SHELF_PRODUCT_SELECTION,
    SORT_SHELF_GROUP

} from "@constants/ActionTypes";
import {deleteAxios, getAxios, postAxios, putAxios} from "@utils/api";

/**
 * 批量删除货架商品分组
 * @param groupId
 * @param ids
 * @param callback
 * @returns {{callback: *, type: string, params: {groupId: *, ids: *}}}
 */
export const bulkDeleteProductFromShelfGroup = ({ groupId, ids }, callback) => ({
    type: apiType(BULK_DELETE_PRODUCT_FROM_SHELF_GROUP).request,
    params: { groupId, ids },
    callback
})

/**
 * 批量添加货架商品分组
 * @param groupIds
 * @param ids
 * @param callback
 * @returns {{callback: *, type: string, params: {groupIds: *, ids: *}}}
 */
export const bulkAddProductToShelfGroup = ({ groupIds, ids }, callback) => ({
    type: apiType(BULK_ADD_PRODUCT_TO_SHELF_GROUP).request,
    params: { groupIds, ids },
    callback
})

/**
 * 批量修改货架商品价格
 * @param prods
 * @param callback
 * @returns {{callback: *, type: string, params: {prods: *}}}
 */
export const bulkEditShelfProductPrice = ({ prods }, callback) => ({
    type: apiType(BULK_EDIT_SHELF_PRODUCT_PRICE).request,
    params: { prods },
    callback
})

/**
 * 编辑上下架货架商品
 * @param ids
 * @param status
 * @param callback
 * @returns {{callback: *, type: string, params: {prods: *}}}
 */
export const bulkEditShelfProductStatus = ({ ids, status }, callback) => ({
    type: apiType(BULK_EDIT_SHELF_PRODUCT_STATUS).request,
    params: { ids, status },
    callback
})

/**
 * 获取货架分组列表
 * @param callback
 * @returns {{callback: *, type: string}}
 */
export const getShelfGroupList = (callback) => ({
    type: apiType(GET_SHELF_GROUP_LIST).request,
    callback
})

/**
 * 删除货架分组
 * @param id
 * @param callback
 * @returns {{callback: *, type: string, params: {id: *}}}
 */
export const deleteShelfGroup = ({ id }, callback) => ({
    type: apiType(DELETE_SHELF_GROUP).request,
    params: {id},
    callback
})

/**
 * 创建货架分组
 * @param name
 * @param callback
 * @returns {{callback: *, type: string, params: {name: *}}}
 */
export const createShelfGroup = ({ name }, callback) => ({
    type: apiType(CREATE_SHELF_GROUP).request,
    params: {name},
    callback
})

/**
 * 编辑货架分组
 * @param id
 * @param name
 * @param callback
 * @returns {{callback: *, type: string, params: {name: *, id: *}}}
 */
export const editShelfGroup = ({ id, name }, callback) => ({
    type: apiType(EDIT_SHELF_GROUP).request,
    params: {id, name},
    callback
})

/**
 * 货架分组排序
 * @param ids
 * @param callback
 * @returns {{callback: *, type: string, params: {ids: *}}}
 */
export const sortShelfGroup = ({ ids }, callback) => ({
    type: apiType(SORT_SHELF_GROUP).request,
    params: {ids},
    callback
})

/**
 * 获取可导入货架的商品列表
 * @param query
 * @param prodTypeId
 * @param fromMenu
 * @param pageNo
 * @param pageSize
 * @param callback
 * @returns {{callback: *, type: string, params: {fromMenu: *, pageNo: *, query: *, pageSize: *, prodTypeId: *}}}
 */
export const getCanImportToShelfProductSelection = ({ query, prodTypeId, fromMenu, pageNo, pageSize }, callback) => ({
    type: apiType(GET_CAN_IMPORT_TO_SHELF_PRODUCT_SELECTION).request,
    params: {query, prodTypeId, fromMenu, pageNo, pageSize},
    callback
})

/**
 * 获取货架商品列表
 * @param groupId
 * @param query
 * @param status
 * @param groupStatus
 * @param pageNo
 * @param pageSize
 * @param callback
 * @returns {{callback: *, type: string, params: {pageNo: *, groupId: *, query: *, pageSize: *, groupStatus: *, status: *}}}
 */
export const getShelfProductList = ({ groupId, query, status, groupStatus, pageNo, pageSize }, callback) => ({
    type: apiType(GET_SHELF_PRODUCT_LIST).request,
    params: { groupId, query, status, groupStatus, pageNo, pageSize },
    callback
})

/**
 * 获取货架商品Selection
 * @param groupId
 * @param query
 * @param status
 * @param groupStatus
 * @param pageNo
 * @param pageSize
 * @param callback
 * @returns {{callback: *, type: string, params: {pageNo: *, groupId: *, query: *, pageSize: *, groupStatus: *, status: *}}}
 */
export const getShelfProductSelection = ({ groupId, query, status, groupStatus, pageNo, pageSize }, callback) => ({
    type: apiType(GET_SHELF_PRODUCT_SELECTION).request,
    params: { groupId, query, status, groupStatus, pageNo, pageSize },
    callback
})

/**
 * 批量删除货架商品
 * @param ids
 * @param callback
 * @returns {{callback: *, type: string, params: {ids: *}}}
 */
export const bulkDeleteShelfProduct = ({ids}, callback) => ({
    type: apiType(BULK_DELETE_SHELF_PRODUCT).request,
    params: { ids },
    callback
})

/**
 * 批量导入商品到货架
 * @param ids
 * @param status
 * @param fromMenu
 * @returns {{callback: *, type: string, params: {fromMenu: *, ids: *, status: *}}}
 */
export const bulkAddShelfProduct = ({ ids, status, fromMenu }, callback) => ({
    type: apiType(BULK_ADD_SHELF_PRODUCT).request,
    params: { ids, status, fromMenu },
    callback
})

/**
 * 获取货架商品详情
 * @param id
 * @param callback
 * @returns {{callback: *, type: string, params: {id: *}}}
 */
export const getShelfProductInfo = ({ id }, callback) => ({
    type: apiType(GET_SHELF_PRODUCT_INFO).request,
    params: { id },
    callback
})

/**
 * 编辑货架商品详情
 * @param id
 * @param customName
 * @param groupIds
 * @param status
 * @param skus
 * @param callback
 * @returns {{callback: *, type: string, params: {skus: *, groupIds: *, customName: *, id: *, status: *}}}
 */
export const editShelfProductInfo = ({ id, customName, groupIds, status, skus }, callback) => ({
    type: apiType(EDIT_SHELF_PRODUCT_INFO).request,
    params: { id, customName, groupIds, status, skus },
    callback
})

/**
 * 获取商城货架可选下拉框
 * @param callback
 * @returns {{callback: *, type: string}}
 */
export const getShelfGroupSelection = (callback) => ({
    type: apiType(GET_SHELF_GROUP_SELECTION).request,
    callback
})

export const checkMenuInfo = (callback) => ({
    type: apiType(CHECK_MENU_INFO).request,
    callback
})

const sagas = {
    * [apiType(BULK_DELETE_PRODUCT_FROM_SHELF_GROUP).request]({params, callback}) {
        try {
            const response = yield deleteAxios('/mall/web/shelf/batch/group', params)
            yield put({
                type: apiType(BULK_DELETE_PRODUCT_FROM_SHELF_GROUP).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(BULK_DELETE_PRODUCT_FROM_SHELF_GROUP).failure});
        }
    },
    * [apiType(BULK_ADD_PRODUCT_TO_SHELF_GROUP).request]({params, callback}) {
        try {
            const response = yield postAxios('/mall/web/shelf/batch/group', params)
            yield put({
                type: apiType(BULK_ADD_PRODUCT_TO_SHELF_GROUP).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(BULK_ADD_PRODUCT_TO_SHELF_GROUP).failure});
        }
    },
    * [apiType(BULK_EDIT_SHELF_PRODUCT_PRICE).request]({params, callback}) {
        try {
            const response = yield postAxios('/mall/web/shelf/batch/price', params)
            yield put({
                type: apiType(BULK_EDIT_SHELF_PRODUCT_PRICE).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(BULK_EDIT_SHELF_PRODUCT_PRICE).failure});
        }
    },
    * [apiType(BULK_EDIT_SHELF_PRODUCT_STATUS).request]({params, callback}) {
        try {
            const response = yield postAxios('/mall/web/shelf/batch/status', params)
            yield put({
                type: apiType(BULK_EDIT_SHELF_PRODUCT_STATUS).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(BULK_EDIT_SHELF_PRODUCT_STATUS).failure});
        }
    },
    * [apiType(GET_SHELF_GROUP_LIST).request]({callback}) {
        try {
            const response = yield getAxios('/mall/web/shelf/group/list')
            yield put({
                type: apiType(GET_SHELF_GROUP_LIST).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(GET_SHELF_GROUP_LIST).failure});
        }
    },
    * [apiType(DELETE_SHELF_GROUP).request]({params, callback}) {
        try {
            const response = yield deleteAxios('/mall/web/shelf/group/list', params)
            yield put({
                type: apiType(DELETE_SHELF_GROUP).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(DELETE_SHELF_GROUP).failure});
        }
    },
    * [apiType(CREATE_SHELF_GROUP).request]({params, callback}) {
        try {
            const response = yield postAxios('/mall/web/shelf/group/list', params)
            yield put({
                type: apiType(CREATE_SHELF_GROUP).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(CREATE_SHELF_GROUP).failure});
        }
    },
    * [apiType(EDIT_SHELF_GROUP).request]({params, callback}) {
        try {
            const response = yield putAxios('/mall/web/shelf/group/list', params)
            yield put({
                type: apiType(EDIT_SHELF_GROUP).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(EDIT_SHELF_GROUP).failure});
        }
    },
    * [apiType(SORT_SHELF_GROUP).request]({params, callback}) {
        try {
            const response = yield postAxios('/mall/web/shelf/group/sort', params)
            yield put({
                type: apiType(SORT_SHELF_GROUP).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(SORT_SHELF_GROUP).failure});
        }
    },
    * [apiType(GET_CAN_IMPORT_TO_SHELF_PRODUCT_SELECTION).request]({params, callback}) {
        try {
            const response = yield getAxios('/mall/web/shelf/import', params)
            yield put({
                type: apiType(GET_CAN_IMPORT_TO_SHELF_PRODUCT_SELECTION).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(GET_CAN_IMPORT_TO_SHELF_PRODUCT_SELECTION).failure});
        }
    },
    * [apiType(GET_SHELF_PRODUCT_LIST).request]({params, callback}) {
        try {
            const response = yield getAxios('/mall/web/shelf/list', params)
            yield put({
                type: apiType(GET_SHELF_PRODUCT_LIST).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(GET_SHELF_PRODUCT_LIST).failure});
        }
    },
    * [apiType(GET_SHELF_PRODUCT_SELECTION).request]({params, callback}) {
        try {
            const response = yield getAxios('/mall/web/shelf/list', params)
            yield put({
                type: apiType(GET_SHELF_PRODUCT_SELECTION).success,
                params,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(GET_SHELF_PRODUCT_SELECTION).failure});
        }
    },
    * [apiType(BULK_DELETE_SHELF_PRODUCT).request]({params, callback}) {
        try {
            const response = yield deleteAxios('/mall/web/shelf/list', params)
            yield put({
                type: apiType(BULK_DELETE_SHELF_PRODUCT).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(BULK_DELETE_SHELF_PRODUCT).failure});
        }
    },
    * [apiType(BULK_ADD_SHELF_PRODUCT).request]({params, callback}) {
        try {
            const response = yield postAxios('/mall/web/shelf/list', params)
            yield put({
                type: apiType(BULK_ADD_SHELF_PRODUCT).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(BULK_ADD_SHELF_PRODUCT).failure});
        }
    },
    * [apiType(GET_SHELF_PRODUCT_INFO).request]({params, callback}) {
        try {
            const response = yield getAxios('/mall/web/shelf/prod', params)
            yield put({
                type: apiType(GET_SHELF_PRODUCT_INFO).success,
                response,
                params
            });
            callback && callback(response)
        } catch (e) {
            yield put({type: apiType(GET_SHELF_PRODUCT_INFO).failure});
        }
    },
    * [apiType(EDIT_SHELF_PRODUCT_INFO).request]({params, callback}) {
        try {
            const response = yield postAxios('/mall/web/shelf/prod', params)
            yield put({
                type: apiType(EDIT_SHELF_PRODUCT_INFO).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(EDIT_SHELF_PRODUCT_INFO).failure});
        }
    },
    * [apiType(GET_SHELF_GROUP_SELECTION).request]({callback}) {
        try {
            const response = yield getAxios('/mall/web/shelf/prod-types')
            yield put({
                type: apiType(GET_SHELF_GROUP_SELECTION).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(GET_SHELF_GROUP_SELECTION).failure});
        }
    },
    * [apiType(CHECK_MENU_INFO).request]({callback}) {
        try {
            const response = yield getAxios('/prod/menu/check')
            yield put({
                type: apiType(CHECK_MENU_INFO).success,
                response,
            });
            callback && callback()
        } catch (e) {
            yield put({type: apiType(CHECK_MENU_INFO).failure});
        }
    },

}

export default Object.keys(sagas).map(item => {
    return function * s() {
        yield takeEvery(item, function *(args) {
            try {
                yield sagas[item](args)
            } catch (e) {
                console.log(e)
            }
        })
    }
})